package com.loong.web.gp4j.component.intercept;

import java.util.Date;

import javax.annotation.Resource;

import com.loong.tool.lang.Dates;
import com.loong.web.gp4j.bean.schema.statistic.StatisticSchema;
import com.loong.web.gp4j.dao.statistic.StatisticDao;
import com.loong.web.suppert.intercept.statistic.StatisticLogger;
import com.loong.web.suppert.intercept.statistic.StatisticVo;

/**
 * 访问统计记录组件
 *
 * @author 张成轩
 */
public class StatisticComponent implements StatisticLogger {

	@Resource
	private StatisticDao statisticDao;

	@Override
	public void save(StatisticVo statisticVo) {

		StatisticSchema statistic = build(statisticVo);
		statisticDao.insert(statistic);
	}

	/**
	 * 构建统计数据对象
	 * 
	 * @param statisticVo 统计对象
	 * @return 统计数据对象
	 */
	private StatisticSchema build(StatisticVo statisticVo) {

		StatisticSchema schema = new StatisticSchema();
		schema.setName(statisticVo.getName());
		schema.setIp(statisticVo.getIp());
		schema.setUrl(statisticVo.getUrl());
		schema.setParam(statisticVo.getParam());
		schema.setClas(statisticVo.getClas().getSimpleName());
		schema.setMethod(statisticVo.getMethod().getName());
		schema.setOs(statisticVo.getEnv().getOs());
		schema.setBrowser(statisticVo.getEnv().getBrowser());
		schema.setBrowserVersion(statisticVo.getEnv().getBrowserVersion());
		schema.setEnv(statisticVo.getEnv().getEnv());
		schema.setTime(Dates.formatTime(new Date(statisticVo.getTime())));
		return schema;
	}
}
